package com.sbw.springboot.demo.controller;

import com.sbw.springboot.demo.async.task.AsyncTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
 * @Author: sangbowei
 * @Description: TODO
 * @Date: Created in  17:45 2017\12\23 0023
 **/
@RestController
@RequestMapping(value="/async")
public class AsyncTaskController {

    @Autowired
    private AsyncTask asyncTask;

    @RequestMapping(value = "/doTask", method = RequestMethod.GET)
    public String doTask() throws InterruptedException{
        long currentTimeMillis = System.currentTimeMillis();
        this.task1();
        this.task2();
        this.task3();
        long currentTimeMillis1 = System.currentTimeMillis();
        System.out.println("normal task任务总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
        return "normal task任务总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms";
    }

    @RequestMapping(value = "/doAsyncTask", method = RequestMethod.GET)
    public String doAsyncTask() throws InterruptedException{
        long currentTimeMillis = System.currentTimeMillis();
        asyncTask.task1();
        asyncTask.task2();
        asyncTask.task3();
        long currentTimeMillis1 = System.currentTimeMillis();
        System.out.println("async task 任务总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
        return "async task 任务总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms";
    }

    @RequestMapping(value = "/doAsyncTask2", method = RequestMethod.GET)
    public String doAsyncTask2() throws InterruptedException{
        long currentTimeMillis = System.currentTimeMillis();
        Future<String> taskRes1 = asyncTask.task11();
        Future<String> taskRes2 = asyncTask.task22();
        Future<String> taskRes3 = asyncTask.task33();
        for (;;) {
            if(taskRes1.isDone() && taskRes2.isDone() && taskRes3.isDone()){
                System.out.println("all tasks have done , you can enter next step!");
                break;
            }
            Thread.sleep(1000);
        }
        long currentTimeMillis1 = System.currentTimeMillis();
        System.out.println("async task 2 任务总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
        return "async task 2 任务总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms";
    }

    @Async
    public void task1() throws InterruptedException{
        long currentTimeMillis = System.currentTimeMillis();
        Thread.sleep(1000);
        long currentTimeMillis1 = System.currentTimeMillis();
        System.out.println("task1任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
    }

    @Async
    public void task2() throws InterruptedException{
        long currentTimeMillis = System.currentTimeMillis();
        Thread.sleep(2000);
        long currentTimeMillis1 = System.currentTimeMillis();
        System.out.println("task2任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
    }
    @Async
    public void task3() throws InterruptedException{
        long currentTimeMillis = System.currentTimeMillis();
        Thread.sleep(3000);
        long currentTimeMillis1 = System.currentTimeMillis();
        System.out.println("task3任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
    }
}